package com.example.tao.smartcar.view.indicator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;

import com.example.tao.smartcar.R;

/**
 * Created by Tao on 2016/8/2.
 */
public class HalfIndicatorView extends View {
    public HalfIndicatorView(Context context) {
        this(context, null);
    }

    public HalfIndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HalfIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private int mValue = 0, mMax = 40, mMin = -40;

    private Paint panelPaint, indicatorPaint;
    private RectF panelRectF, centerPointRectF, indicatorRectF;
    private PointF centerPointF;

    private int viewHeight, viewWidth, panelRadius;
    private int centerRadius;
    private int panelStrokeWidth, bigScaleWidth, smallScaleWidth, bigScaleLength, smallScaleLength, indicatorWidth, indicatorLength;

    private int indicatorDegree = 0;

    private void init() {
        panelPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);

        indicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        indicatorPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        indicatorPaint.setColor(getResources().getColor(R.color.white_80_alpha));

        centerPointF = new PointF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = h;
        viewWidth = w;
        if (viewWidth > 2 * viewHeight) {
            panelRadius = (int) (0.75 * viewHeight);
            centerPointF.set(viewWidth / 2, (int)(viewHeight * 0.9));
        } else {
            panelRadius = (int) (0.75 * viewWidth / 2);
            centerPointF.set(viewWidth / 2, panelRadius + (viewHeight - panelRadius) / 3);
        }
        panelStrokeWidth = (int) (0.1 * panelRadius);
        bigScaleWidth = (int) (0.04 * panelRadius);
        smallScaleWidth = (int) (0.02 * panelRadius);
        bigScaleLength = (int) (0.2 * panelRadius);
        smallScaleLength = (int) (0.15 * panelRadius);
        indicatorWidth = (int) (0.08 * panelRadius);
        indicatorLength = (int) (0.8 * panelRadius);
        centerRadius = (int) (0.1 * panelRadius);
        invalidateView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawPanel(canvas);
    }

    private void drawBackground(Canvas canvas) {
        canvas.drawColor(getResources().getColor(R.color.white_00_alpha));
    }

    private void drawPanel(Canvas canvas) {
        panelPaint.setStyle(Paint.Style.STROKE);
        panelPaint.setColor(getResources().getColor(R.color.white_ff_alpha));
        panelPaint.setStrokeWidth(panelStrokeWidth);
        panelRectF = new RectF(centerPointF.x - panelRadius, centerPointF.y - panelRadius, centerPointF.x + panelRadius, centerPointF.y + panelRadius);
        canvas.drawArc(panelRectF, -190, 200, false, panelPaint);

        drawScale(canvas);
    }

    private void drawScale(Canvas canvas) {
        for (int i = 0; i <= 180; ) {
            canvas.save();
            canvas.rotate(i, centerPointF.x, centerPointF.y);
            if (i % 30 == 0) {
                panelPaint.setStrokeWidth(bigScaleWidth);
                canvas.drawLine(centerPointF.x - panelRadius, centerPointF.y, centerPointF.x - panelRadius + bigScaleLength, centerPointF.y, panelPaint);
            } else {
                panelPaint.setStrokeWidth(smallScaleWidth);
                canvas.drawLine(centerPointF.x - panelRadius, centerPointF.y, centerPointF.x - panelRadius + smallScaleLength, centerPointF.y, panelPaint);
            }
            canvas.restore();
            i = i + 6;
        }
        drawIndicator(canvas);
    }

    private void drawIndicator(Canvas canvas) {
        panelPaint.setStrokeWidth(indicatorWidth);
        canvas.save();
        canvas.rotate(indicatorDegree, centerPointF.x, centerPointF.y);
        canvas.drawLine(centerPointF.x, centerPointF.y, centerPointF.x, centerPointF.y - indicatorLength, panelPaint);
        canvas.restore();

        panelPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        centerPointRectF = new RectF(centerPointF.x - centerRadius, centerPointF.y - centerRadius, centerPointF.x + centerRadius, centerPointF.y + centerRadius);
        canvas.drawArc(centerPointRectF, -180, 360, false, panelPaint);
    }

    public void setValue(int value) {
        this.mValue = value;
        updateIndicator();
    }

    public void setMax(int value) {
        this.mMax = value;
        updateIndicator();
    }

    public void setMin(int value) {
        this.mMin = value;
        updateIndicator();
    }

    private void updateIndicator() {
        indicatorDegree = (int)(((float)(mValue - mMin))/((float)(mMax - mMin)) * 180.0f - 90.0f);
        invalidateView();
    }

    private void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }
}
